package com.gancp.controller.base;


import com.gancp.cache.CommonCacheUtil;
import com.gancp.common.constants.Constants;
import com.gancp.vo.UserSessionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author gancp
 * @Date 时间被黑洞吸走了
 * @Descripition 控制层基类
 **/
public class BaseController {

    @Autowired
    private CommonCacheUtil cacheUtil;

    /**
     * @Author gancp
     * @Date 时间被黑洞吸走了
     * @Descripition 获取当前线程的request
     **/
    protected HttpServletRequest getHttpServletRequestRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    /**
     * @Author gancp
     * @Date 时间被黑洞吸走了
     * @Descripition 获取当前用户的token
     **/
    protected String getUserToken() {
        String token = getHttpServletRequestRequest().getHeader( Constants.REQUEST_HEADER_TOKEN );
        return token;
    }

    /**
     * @Author gancp
     * @Date 时间被黑洞吸走了
     * @Descripition 获取当前用户的sessionVo
     **/
    protected UserSessionVo getUserSessionVo() {
        String token = getUserToken();
        UserSessionVo userSessionVo = null;
        if (StringUtils.isNotBlank( token )) {
            userSessionVo = cacheUtil.getUserByToken( token );
        }
        return userSessionVo;
    }
}
